/**
 * 个人资料
 * @author  Jello
 * @create  2018/10/17 15:50
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var sessionStorage = window.sessionStorage;
var spinner = new Spinner(opts);
var userImage = ""; //用户头像
var userid_sign = ""; //用户登录标识

$(function() {
	userid_sign = sessionStorage.getItem("userid_sign");
	//图像、昵称、性别
	var user_url = sessionStorage.getItem("user_url");
	if(user_url == "" || user_url == null) {
		$("#userurl").attr("src", "../../img/personal/us_elim2.png");
	} else {
		$("#userurl").attr("src", user_url);
	}
	$("#username").val(sessionStorage.getItem("user_name"));
	var user_sex = sessionStorage.getItem("user_sex")
	if(user_sex == 0 || user_sex == 1) { //男
		$("#male").attr("checked", "checked");
	} else {
		$("#girl").attr("checked", "checked");
	}

})

/**
 * 上传头像
 * @param {Object} obj
 */
function start_upload(obj) {
	if(!isCanvasSupported) {

	} else { //把base64转成图片
		compress(event, function(base64Img) {
			//如需保存或把base64转成图片  ajax提交后台处理
			$("#userurl").attr("src", base64Img);
			//截取以，分割
			var base64Ime_split = base64Img.split(",");
			var split_base64 = base64Ime_split[1];
			$.ajax({
				type: "post",
				url: serverUrl + "/platform/upload",
				data: "{\"file\":\"" + split_base64 + "\"}",
				cache: false,
				async: false,
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				crossDomain: true,
				timeout: 15000,
				beforeSend: function() {
					$("#myspin").text("");
					var target = $("#myspin").get(0);
					spinner.spin(target);
				},
				success: function(data) {
					var imgObj = eval(data);
					if(imgObj.code == 0 && imgObj.data != null) {
						spinner.spin();
						userImage = imgObj.data;
					} else {
						mui.toast(imgObj.msg);
						return;
					}
				},
				error: function(XMLHttpRequest, status, errorThrown) {
					spinner.spin();
					if(status == 'timeout') {
						mui.toast("请求超时");
					} else {
						mui.toast("请求网络服务数据失败");
					}
					return;
				}
			});
		});
	}
}
 
/**
 * 确定修改个人资料
 */
function updateMaterial() {
	var sex = $('input:radio[name="radioname"]:checked').val(); //当前选择的性别
	backLogin(userid_sign, "../../login.html", function() {
		var nickname = $("#username").val();
		var flag = checkNickname(nickname);
		if(flag) {
			$.ajax({
				type: "post",
				url: serverUrl + "/userMaterial/saveOrUpdate?sgin=" + userid_sign + "&userName=" + nickname + "&userSex=" + sex + "&userUrl=" + userImage,
				data: "{\"type\":\"" + 1 + "\"}",
				cache: false,
				async: false,
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				crossDomain: true,
				timeout: 15000,
				beforeSend: function() {
					$("#myspin").text("");
					var target = $("#myspin").get(0);
					spinner.spin(target);
				},
				success: function(data) {
					spinner.spin();
					var materialObj = eval(data);
					if(materialObj.code == 0 && materialObj.data == 1) {
						mui.toast("修改个人资料成功");
						setTimeout(function() {
							window.location.href = "personal.html";
						}, 100);
					}else{
						mui.toast(data.msg);
						return;
					}
				},
				error: function(XMLHttpRequest, status, errorThrown) {
					spinner.spin();
					if(status == 'timeout') {
						mui.toast("请求超时");
					} else {
						mui.toast("请求网络服务数据失败");
					}
					return;
				}
			});
		}
	})
}

/**
 * 校验用户昵称
 * @param {Object} nickname
 */
function checkNickname(nickname) {
	if(nickname == "") {
		mui.toast("请输入昵称");
		return;
	} else if(nickname.length > 11) {
		mui.toast("昵称长度限制在11位以内");
		return;
	}
	return true;
}